//
//  GDMapTool.m
//  KEREN
//
//  Created by MW on 2023/3/13.
//

#import "GDMapTool.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@implementation GDMapTool

+(instancetype)sharedInstance{
    static GDMapTool *gdMapTool = nil;
    if (gdMapTool == nil) {
        gdMapTool = [[GDMapTool alloc] init];
    }
    return gdMapTool;
}
+(BOOL)isContainsLocation:(CLLocationCoordinate2D )location withCenterLocation:(CLLocationCoordinate2D )centerLocation{
    BOOL isContains = MACircleContainsCoordinate(location, centerLocation, 200);
    return isContains;
}

- (CLLocationCoordinate2D)AMapCoordinateConvertWithLatitude:(float)latitude andLongitude:(float)longitude{
    CLLocationCoordinate2D amapcoord = AMapCoordinateConvert(CLLocationCoordinate2DMake(latitude,longitude), AMapCoordinateTypeGPS);
    return amapcoord;

}
-(void)getCurrentLocation:(GDMapBlock)map{
    WEAKSELF
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    //   定位超时时间，最低2s，此处设置为10s
    self.locationManager.locationTimeout =2;
    //   逆地理请求超时时间，最低2s，此处设置为10s
    self.locationManager.reGeocodeTimeout = 2;
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error)
        {
            DDLogInfo(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            if (error.code == AMapLocationErrorLocateFailed)
            {
                [[KRAlartManager shareInstance] showErrorAlert:@"获取地理位置失败，请检查定位权限或网络"];
                return;
            }
        }
        
        DDLogInfo(@"location:%@", location);
        
        if (regeocode)
        {
            weakSelf.address = regeocode.formattedAddress;
            weakSelf.adcode = regeocode.adcode;
            DDLogInfo(@"reGeocode:%@", regeocode);
        }
        if(!regeocode){
            return;
        }
        if(regeocode.adcode && regeocode.adcode.length > 0){
            NSDictionary *parameters = @{@"key":GDWebKey,@"city":regeocode.adcode,@"extensions":@"base"};
            weakSelf.location = location;
            [NetworkingTool GET:@"https://restapi.amap.com/v3/weather/weatherInfo" parameters:parameters isDeviceUrl:NO success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
                if(response[@"lives"] && [response[@"lives"] isKindOfClass:[NSArray class]]){
                    NSDictionary *dict = response[@"lives"][0];
                    weakSelf.weather = dict[@"weather"];
                    weakSelf.temperature = dict[@"temperature"];
                    weakSelf.windpower = [NSString stringWithFormat:@"%@风 %@级",dict[@"winddirection"],dict[@"windpower"]] ;
                    weakSelf.humidity = dict[@"humidity"];
                }
                map();
            } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
                
            }];
        }
    }];

}

//- (void)getLocation{
//    WEAKSELF
//    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//    //   定位超时时间，最低2s，此处设置为10s
//    self.locationManager.locationTimeout =2;
//    //   逆地理请求超时时间，最低2s，此处设置为10s
//    self.locationManager.reGeocodeTimeout = 2;
//    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
//        if (error)
//        {
//            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
//            
//            if (error.code == AMapLocationErrorLocateFailed)
//            {
//                return;
//            }
//        }
//        NSLog(@"location:%@", location);
//        weakSelf.location = location;
//    }];
//
//}

- (AMapLocationManager *)locationManager{
    if(!_locationManager){
        _locationManager = [[AMapLocationManager alloc] init];
        _locationManager.delegate = self;
    }
    return _locationManager;
}


@end
